
package com.yimixian.app.order;

import com.squareup.picasso.Picasso;
import com.ta.utdid2.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.fragment.OrderMvpFragment;
import com.yimixian.app.model.GoodImage;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.Metadata;
import com.yimixian.app.model.Order;
import com.yimixian.app.net.HttpApi;
import com.yimixian.app.ui.JiShiSongProgressBar;
import com.yimixian.app.ui.PercentView;
import com.yimixian.app.ui.ProgressLineView;
import com.yimixian.app.util.CustomViewUtils;
import com.yimixian.app.util.DateUtil;
import com.yimixian.app.util.Strings;
import com.yimixian.app.util.UiUtils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class OrderListItemView extends LinearLayout implements View.OnClickListener{

    @InjectView(R.id.order_time)
    TextView mOrderTimeTextView;
    @InjectView(R.id.order_number)
    TextView mOrderNumberTextView;
    @InjectView(R.id.order_quantity)
    TextView mOrderQuantityTextView;
    @InjectView(R.id.order_price)
    TextView mOrderPriceTextView;
    @InjectView(R.id.pick_up_code)
    TextView mPickUpCodeTextView;
    @InjectView(R.id.order_complain_button)
    TextView mOrderComplainTextView;
    @InjectView(R.id.order_uninvalid_button)
    TextView morderUninvalidButton;
    @InjectView(R.id.order_comment_button)
    TextView mOrderCommentTextView;
    @InjectView(R.id.order_balance)
    TextView order_balance;
    @InjectView(R.id.progress_line_view)
    ProgressLineView mProgressLineView;
    Handler mHandler;

    @InjectView(R.id.btn_zfb_pay)
    Button btn_zfb_pay;
    @InjectView(R.id.btn_wx_pay)
    Button btn_wx_pay;
    @InjectView(R.id.tv_tip)
    TextView tv_tip;
    @InjectView(R.id.tv_pay_count_down)
    TextView tv_pay_count_down;
    @InjectView(R.id.ll_pic)
    LinearLayout ll_pic;
    @InjectView(R.id.ll_jishisong)
    LinearLayout ll_jishisong;
    @InjectView(R.id.v_jishisong)
    JiShiSongProgressBar v_jishisong;
    @InjectView(R.id.tv_jishisong)
    PercentView tv_jishisong;
    @InjectView(R.id.v_bottom)
    View v_bottom;
    @InjectView(R.id.ll_btns)
    LinearLayout ll_btns;
    @InjectView(R.id.iv_car)
    ImageView iv_car;
    @InjectView(R.id.iv_right_arrow1)
    ImageView iv_right_arrow1;
    @InjectView(R.id.iv_right_arrow2)
    ImageView iv_right_arrow2;
    @InjectView(R.id.tv_temp)
    TextView tv_temp;

    private int totalSeconds; //总共时间
    private int useSeconds;  //已经用时
    private DataManager mDataManager;
    private String payMethod, payMentId;
    private CountDownTimer timer;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int minute = msg.arg1;
            int second = msg.arg2;
            tv_pay_count_down.setText("支付倒计时 " +
                    (minute < 10 ? "0" + minute : minute) + ":" +
                    (second < 10 ? "0" + second : second));
            if(second == 0 && minute == 0) {
                setUninvalidUi(0);
                timer.isStop = true;
                tv_pay_count_down.setText("支付超时");
            }
        }
    };

    public OrderListItemView(Context context, Handler mHandler) {
        super(context);
        this.mHandler = mHandler;
        initOrderListItemView();
    }

    public OrderListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initOrderListItemView();
    }

    public OrderListItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initOrderListItemView();
    }

    private void initOrderListItemView() {
        setOrientation(VERTICAL);
        LayoutInflater.from(getContext()).inflate(R.layout.order_list_item_view, this);
        ButterKnife.inject(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
        mDataManager = DataManager.getInstance();
    }

    public void bind(final Order order) {
        //订单号
        SpannableStringBuilder orderNumberBuilder = new SpannableStringBuilder("订单号：");
        orderNumberBuilder.append(String.valueOf(order.id));
        mOrderNumberTextView.setText(orderNumberBuilder);
        boolean isShowPickUpCode = false;
        //订单创建时间
        mOrderTimeTextView.setText(order.createdAt);
        //水果数量
        mOrderQuantityTextView.setText("共" + order.totalCount + "份商品");


//        for (Metadata metadata : order.metadata) {
//            if ("CREATED_AT".equals(metadata.rawName)) {
//                mOrderTimeTextView.setText(metadata.value);
//            } else if ("DELIVERY_VERIFY_CODE".equals(metadata.rawName)) {
//                mPickUpCodeTextView.setText(metadata.name + ": " + metadata.value);
//                isShowPickUpCode = true;
//            }
//        }
        if (isShowPickUpCode) {
            mPickUpCodeTextView.setVisibility(View.VISIBLE);
        } else {
            mPickUpCodeTextView.setVisibility(View.GONE);
        }
        //支付总金额
        mOrderPriceTextView.setText(order.totalPrice);

        setUninvalidUi(order.status);

        if (!Strings.isNullOrEmpty(order.complainUrl)) {
            mOrderComplainTextView.setVisibility(View.VISIBLE);
            mOrderComplainTextView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHandler != null) mHandler.sendEmptyMessage(0);
                    MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                            StatConstants.RECENT_ORDER_ITEM_APPLY_CUSTOM_SERVICE_CLICK);
                    Intent intent = new Intent(getContext(), WebViewActivity.class);
                    intent.putExtra(WebViewActivity.EXTRA_URL, order.complainUrl);
                    getContext().startActivity(intent);
                }
            });
        } else {
            mOrderComplainTextView.setVisibility(View.GONE);
        }

        if (!StringUtils.isEmpty(order.commentUrl) &&
                !StringUtils.isEmpty(order.commentBtnName)) {
            mOrderCommentTextView.setVisibility(View.VISIBLE);
            mOrderCommentTextView.setText(order.commentBtnName);
            mOrderCommentTextView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHandler != null) mHandler.sendEmptyMessage(0);
                    MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                            StatConstants.RECENT_ORDER_ITEM_COMMENT_CUSTOM);
                    Intent intent = new Intent(getContext(), WebViewActivity.class);
                    intent.putExtra(WebViewActivity.EXTRA_URL, order.commentUrl);
                    getContext().startActivity(intent);
                }
            });
        } else {
            mOrderCommentTextView.setVisibility(View.GONE);
        }
        Order.DeliveryInfo deliveryInfo = order.deliveryInfo;
        mProgressLineView.setProgressLineItems(deliveryInfo.statuses, deliveryInfo.index, order.status);

        //2.3.0相关
        btn_zfb_pay.setOnClickListener(this);
        btn_wx_pay.setOnClickListener(this);
        order_balance.setOnClickListener(this);

//        final int leftSeconds = 10 + (int) (21 * Math.random());
//        v_jishisong.startProgress(leftSeconds, 5);
//        v_jishisong.setBackColor(0xffd8d8d8);
//        v_jishisong.setOnProgressMoveListener(new JiShiSongProgressBar.OnProgressMove() {
//            @Override
//            public void onMove(float percent) {
//                int showTime = (int) Math.ceil(leftSeconds * (1 - percent));
//                tv_jishisong.setText("配送超时赔付 00:" + (showTime < 10 ? "0" + showTime : showTime));
//                if (percent == 1) {
//                    ScaleAnimation scale = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//                    scale.setDuration(400);
//                    scale.setAnimationListener(new Animation.AnimationListener() {
//                        @Override
//                        public void onAnimationStart(Animation animation) {
//                        }
//
//                        @Override
//                        public void onAnimationEnd(Animation animation) {
//                            tv_tip.setVisibility(View.VISIBLE);
//                            ll_jishisong.setVisibility(View.GONE);
//                            ScaleAnimation scale = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//                            scale.setDuration(400);
//                            scale.setInterpolator(new OvershootInterpolator(1.2f));
//                            tv_tip.startAnimation(scale);
//                        }
//
//                        @Override
//                        public void onAnimationRepeat(Animation animation) {
//                        }
//                    });
//                    ll_jishisong.startAnimation(scale);
//                }
//            }
//        });

        //支付倒计时
        tv_pay_count_down.setVisibility(View.GONE);

        if(deliveryInfo.index < 1){
            boolean timeFlag = false;
            if(!Strings.isNullOrEmpty(order.paymentDueAt)){
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate = new Date();
                    Date targetDate = sdf.parse(order.paymentDueAt);
                    long delta = targetDate.getTime() - curDate.getTime();
                    if(delta > 0){
                        timeFlag = true;
                        tv_pay_count_down.setVisibility(View.VISIBLE);
                        int seconds = (int) Math.ceil(delta / 1000);
                        if(timer != null){
                            timer.isStop = true;
                        }
                        timer = new CountDownTimer(seconds);
                        timer.start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(!timeFlag){
                tv_pay_count_down.setText("");
            }
        }

        //加入图片
        if(order.goodImages == null || order.goodImages.size() == 0){
            iv_right_arrow1.setVisibility(View.VISIBLE);
            iv_right_arrow2.setVisibility(View.INVISIBLE);
            ll_pic.setVisibility(View.GONE);
        }else{
            iv_right_arrow1.setVisibility(View.INVISIBLE);
            iv_right_arrow2.setVisibility(View.VISIBLE);
            ll_pic.setVisibility(View.VISIBLE);
            ll_pic.removeAllViews();
            float dentity = getResources().getDisplayMetrics().density;
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            if(order.goodImages != null){
                for (int i = 0; i < order.goodImages.size(); i++) {
                    GoodImage image = order.goodImages.get(i);
                    FrameLayout parent = new FrameLayout(getContext());
                    parent.setPadding((int) (1 * dentity), (int) (1 * dentity), (int) (1 * dentity), (int) (1 * dentity));
                    parent.setBackgroundColor(0xffececec);
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) (51 * dentity), (int) (51 * dentity));
                    if(i == 0)
                        lp.setMargins(0, 0, 0, 0);
                    else
                        lp.setMargins((int) (10 * dentity), 0, 0, 0);
                    ImageView iv = new ImageView(getContext());
                    if(i < 4){
                        Picasso picasso = Picasso.with(getContext());
                        picasso.load(mDataManager.getImageUrl(image.img, screenWidth / 5))
                                .placeholder(R.drawable.icon_home_item_default).into(iv);
                        parent.addView(iv);
                        ll_pic.addView(parent, lp);
                    }else{
                        iv.setImageResource(R.drawable.icon_sheng);
                        FrameLayout tempView = new FrameLayout(getContext());
                        tempView.setBackgroundColor(Color.WHITE);
                        tempView.setPadding((int) (10 * dentity), (int) (10 * dentity), (int) (10 * dentity), (int) (10 * dentity));
                        tempView.addView(iv);
                        parent.setPadding(0, 0, 0, 0);
                        parent.addView(tempView);
                        ll_pic.addView(parent, lp);
                        break;
                    }
                }
            }
        }

        //支付按钮位置
        btn_zfb_pay.setVisibility(View.GONE);
        btn_wx_pay.setVisibility(View.GONE);
        order_balance.setVisibility(View.GONE);
        List<Order.PaymentMethods> list = order.paymentMethods;
        if(list != null){
            int index = 0;
            for (int i = 0; i < ll_btns.getChildCount(); i++) {
                if(ll_btns.getChildAt(i) == btn_zfb_pay){
                    index = i;
                    break;
                }else if(ll_btns.getChildAt(i) == btn_wx_pay){
                    index = i;
                    break;
                }
            }
            ll_btns.removeView(btn_zfb_pay);
            ll_btns.removeView(btn_wx_pay);
            for (int i = 0; i < list.size(); i++) {
                Order.PaymentMethods each = list.get(i);
                if(ConfirmOrderActivity.PAYMENT_METHOD_WECHAT.equals(each.getId())){
                    ll_btns.addView(btn_wx_pay, index + i);
                    btn_wx_pay.setTag(each.getId());
                    btn_wx_pay.setVisibility(View.VISIBLE);
                }else if(ConfirmOrderActivity.PAYMENT_METHOD_ALIPAY.equals(each.getId())){
                    ll_btns.addView(btn_zfb_pay, index + i);
                    btn_zfb_pay.setTag(each.getId());
                    btn_zfb_pay.setVisibility(View.VISIBLE);
                }else if(ConfirmOrderActivity.PAYMENT_METHOD_BALANCE.equals(each.getId())){
                    order_balance.setVisibility(View.VISIBLE);
                    order_balance.setTag(each.getId());
                }
            }
        }

        tv_jishisong.setFontSize(13);
        tv_jishisong.setNormalColor(getResources().getColor(R.color.ymx_orange));
        tv_jishisong.setOverrideColor(Color.WHITE);
        //配送时间
        setJiShiSong(order);

        payMentId = order.paymentId;
    }

    void setJiShiSong(final Order order){
        tv_tip.setVisibility(View.GONE);
        ll_jishisong.setVisibility(View.GONE);
        if(order.speedOrder != null && order.speedOrder.total_time != 0 && order.speedOrder.used_time != 0 && !"待退款".equals(order.deliveryInfo.statuses.get(order.deliveryInfo.index))){

            if(order.speedOrder.sys_time != 0){
                int deltaTime = (int) (Math.ceil(System.currentTimeMillis() - order.speedOrder.sys_time) / 1000);
                order.speedOrder.used_time += deltaTime;
            }

            totalSeconds = order.speedOrder.total_time;
            useSeconds = order.speedOrder.used_time;

            v_bottom.setVisibility(View.VISIBLE);
            if(order.speedOrder.is_completed){
                tv_tip.setVisibility(View.VISIBLE);
                if(order.speedOrder.message.contains("used_time")){
                    int minitue = useSeconds / 60;
                    int second = useSeconds % 60;
                    String minitueStr = minitue < 10 ? "0" + minitue : minitue + "";
                    String secondStr = second < 10 ? "0" + second : second + "";
                    tv_tip.setText(order.speedOrder.message.replaceAll("\\{used_time\\}", minitueStr + ":" + secondStr));
                }else{
                    tv_tip.setText(order.speedOrder.message);
                }
            }else{
                ll_jishisong.setVisibility(View.VISIBLE);
                iv_car.setVisibility(View.INVISIBLE);
                v_jishisong.startProgress(totalSeconds - useSeconds, useSeconds);
                v_jishisong.setBackColor(0xfff0f0f0);
                v_jishisong.setOnProgressMoveListener(new JiShiSongProgressBar.OnProgressMove() {
                    @Override
                    public void onMove(float percent) {
                        //更新小车位置
                        float density = getContext().getResources().getDisplayMetrics().density;
                        float curBarPosition = v_jishisong.getWidth() * percent;
                        float deltaX = curBarPosition - (iv_car.getWidth() / 2 + 2 * density);
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv_car.getLayoutParams();
                        if(deltaX > 0 && lp.leftMargin != (int) deltaX){
                            if(deltaX + iv_car.getWidth() <= v_jishisong.getWidth()){
                                lp.setMargins((int) (deltaX), lp.topMargin, lp.rightMargin, lp.bottomMargin);
                            }else{
                                lp.setMargins(v_jishisong.getWidth() - iv_car.getWidth(), lp.topMargin, lp.rightMargin, lp.bottomMargin);
                            }
                            iv_car.requestLayout();
                        }

                        order.speedOrder.sys_time = System.currentTimeMillis();
                        useSeconds = order.speedOrder.used_time = (int) Math.ceil(totalSeconds * percent);
                        int minitue = (totalSeconds - useSeconds) / 60;
                        int second = (totalSeconds - useSeconds) % 60;
                        String minitueStr = minitue < 10 ? "0" + minitue : minitue + "";
                        String secondStr = second < 10 ? "0" + second : second + "";
                        tv_jishisong.setProgressText(order.speedOrder.message.replaceAll("\\{left_time\\}", minitueStr + ":" + secondStr));
                        tv_jishisong.setPercent(percent);
                        tv_temp.setText("1");
                        if(iv_car.getVisibility() != View.VISIBLE){
                            tv_temp.addTextChangedListener(new TextWatcher() {
                                @Override
                                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                                }

                                @Override
                                public void onTextChanged(CharSequence s, int start, int before, int count) {
                                }

                                @Override
                                public void afterTextChanged(Editable s) {
                                    tv_temp.removeTextChangedListener(this);
                                    iv_car.setVisibility(View.VISIBLE);
                                    AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
                                    alpha.setDuration(300);
                                    iv_car.startAnimation(alpha);
                                }
                            });
                        }
                        if (percent == 1) {
                            //到时间再次对比状态
                            Map<String, String> map = new HashMap<>();
                            map.put("token", DataManager.getInstance().get(CacheKeys.TOKEN) + "");
                            map.put("order_id", order.id + "");
                            HttpApi.startGetRequest("/v5/orders/speed_order_status", map, new HttpApi.RequestCallBack() {
                                @Override
                                public void requestSuccess(String result) {
                                    JSONObject json = null;
                                    try {
                                        json = new JSONObject(result);
                                        json = json.optJSONObject("speed_order");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    if (json == null) {
                                        return;
                                    }

                                    order.speedOrder.used_time = json.optInt("used_time");
                                    order.speedOrder.total_time = json.optInt("total_time");
                                    order.speedOrder.message = json.optString("message");

                                    if (order.speedOrder.used_time >= order.speedOrder.total_time) {
                                        ScaleAnimation scale = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                                        scale.setDuration(400);
                                        scale.setAnimationListener(new Animation.AnimationListener() {
                                            @Override
                                            public void onAnimationStart(Animation animation) {
                                            }

                                            @Override
                                            public void onAnimationEnd(Animation animation) {
                                                tv_tip.setVisibility(View.VISIBLE);
                                                ll_jishisong.setVisibility(View.GONE);
                                                v_bottom.setVisibility(View.VISIBLE);
//                                                if("催单".equals(order.commentBtnName)){
//                                                    mOrderCommentTextView.setVisibility(View.GONE);
//                                                }
                                                ScaleAnimation scale = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                                                scale.setDuration(400);
                                                scale.setInterpolator(new OvershootInterpolator(1.2f));
                                                tv_tip.startAnimation(scale);
                                                if (order.speedOrder.message.contains("used_time")) {
                                                    tv_tip.setText(order.speedOrder.message.replaceAll("\\{used_time\\}", useSeconds + ""));
                                                } else {
                                                    tv_tip.setText(order.speedOrder.message);
                                                }
                                            }

                                            @Override
                                            public void onAnimationRepeat(Animation animation) {
                                            }
                                        });
                                        ll_jishisong.startAnimation(scale);
                                    } else {
                                        setJiShiSong(order);
                                    }
                                }

                                @Override
                                public void requestError() {
                                }

                                @Override
                                public void alwaysDo() {

                                }
                            });

                        }
                    }
                });


            }
        }


    }

    void setUninvalidUi(int status){
        if (status == 0){//无效订单
            morderUninvalidButton.setVisibility(View.VISIBLE);
            mOrderTimeTextView.setTextColor(getContext().getResources().getColor(R.color.cart_item_unit_des));
            mOrderTimeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.order_list_shop_icon_invalid, 0, 0, 0);
            mOrderNumberTextView.setTextColor(getContext().getResources().getColor(R.color.cart_item_unit_des));
            mOrderQuantityTextView.setTextColor(getContext().getResources().getColor(R.color.cart_item_unit_des));
            mOrderPriceTextView.setTextColor(getContext().getResources().getColor(R.color.cart_item_unit_des));

            tv_tip.setVisibility(View.GONE);
            ll_jishisong.setVisibility(View.GONE);
            v_jishisong.setOnProgressMoveListener(null);
            btn_zfb_pay.setVisibility(View.GONE);
            btn_wx_pay.setVisibility(View.GONE);
            order_balance.setVisibility(View.GONE);
            v_bottom.setVisibility(View.GONE);

        }else{
            morderUninvalidButton.setVisibility(View.GONE);
            mOrderTimeTextView.setTextColor(getContext().getResources().getColor(R.color.home_black_color));
            mOrderTimeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.order_list_shop_icon, 0, 0, 0);
            mOrderNumberTextView.setTextColor(getContext().getResources().getColor(R.color.ymx_orange));
            mOrderQuantityTextView.setTextColor(getContext().getResources().getColor(R.color.home_black_color));
            mOrderPriceTextView.setTextColor(getContext().getResources().getColor(R.color.home_black_color));
        }
    }

    @Override
    public void onClick(View v) {
        payMethod = (String) v.getTag();

        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("EVENT", payMethod.toLowerCase());
        DateUtil.setCityUMeng(hashMap, UMengConstants.NEW_ORDERLIST_PAY);

        Intent intent = new Intent(getContext(), OrderPayActivity.class)
                .putExtra("payMethod", payMethod)
                .putExtra("mPaymentId", payMentId);
        ((Activity)getContext()).startActivity(intent);
    }

    class CountDownTimer extends Thread{

        public boolean isStop = false;
        private int seconds;

        public CountDownTimer(int seconds) {
            this.seconds = seconds;
        }

        @Override
        public void run() {
            while(!isStop){
                try {
                    int minute = seconds / 60;
                    int second = seconds % 60;
                    Message msg = new Message();
                    msg.arg1 = minute;
                    msg.arg2 = second;
                    handler.sendMessage(msg);
                    seconds--;
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
